寻找一个数组中具体的某个数字

#include<stdio.h>//方法一
int main()
{
	char arr[] = { 1,2,3,4,5,6,7,8,9, };
	int k = 7;
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
		if (arr[i] == k)
		{
			printf("找到了，是%d", arr[i]);
			break;
		}
	if (i == sz)
	{
		printf("没找到");
	}

	return 0;
}

#include<stdio.h>//方法二(二分查找)
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 7;
	int sz = sizeof(arr) / sizeof(arr[0]);
	int left = 0;
	int right = sz - 1;

	while (left <= right)
	{
		int mid = (right + left) / 2;
		//也可写为：int mid = left + (right - left)/2  (可防止因为值过大导致溢出整型所能表达的最大值)
		if (k > arr[mid])
		{
			left = mid + 1;
		}
		else if (k < arr[mid])
		{
			right = mid - 1;
		}
		else
		{
			printf("找到了，下标是%d", mid);
			break;
		}
	}
	if (left > right)
		printf("找不到");

	return 0;
}